package com.woniuxy.service.impl;

import com.woniuxy.apis.MyhProductApi;
import com.woniuxy.entity.Order;
import com.woniuxy.entity.Product;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.mapper.OrderMapper;
import com.woniuxy.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 马宇航
 * @Todo: TODO
 * @DateTime: 25/07/31/星期四 10:37
 * @Component: 成都蜗牛学苑
 **/
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    MyhProductApi myhProductApi;

    @Override
    public Order getOneById(Integer oid) {
//        ResponseEntity byStore = myhProductApi.findByStore(10, 1000, 1, 1000);
        Product product = myhProductApi.getProduct(oid);
//        System.out.println(byStore.get("body"));
        Order order = orderMapper.getReferenceById(oid);
        order.setUsername(product.getName());
        return order;
    }
    /**
     * 后面全程围绕这个方法来开发，和演示。
     * order订单--->product服务，触发商品查询和扣减
     * ChangeLog : 1. 创建 (25/07/31/星期四 11:38 [马宇航]);
     * @param pid
     * @param num
     * @return void
    */
    @Override
    @GlobalTransactional
    public Order save(Integer pid, Integer num) throws RuntimeException {
        ResponseEntity responseEntity = null;
        //就可以实现一个远程的调用
            Product product = myhProductApi.getProduct(pid);
            //新增订单 优先-->更新库存
            Order order = new Order();
            order.setPid(pid);
            order.setUsername("admin");
            order.setUid(1);
            order.setPrice(product.getPrice());
            order.setPname(product.getName());
            order.setNumber(num);
            orderMapper.save(order);
            System.out.println("订单新增成功");
            //调用put方法，去更新我们的product的库存, 内部降级
            responseEntity = myhProductApi.updateProduct(pid, num);
            return order;

    }


}
